package com.my.springboot.tuling.concurrent.demo.priority;

/**
 * 注意：使用中断机制时一定要注意是否存在中断标志位被清除的情况
 * 处于休眠中的线程被中断，线程是可以感受到中断信号的，并且会抛出一个
 * InterruptedException 异常，同时清除中断信号，将中断标记位设置成 false。
 */
public class StopThread implements Runnable {

    @Override
    public void run() {
        int count = 0;
        while (!Thread.currentThread().isInterrupted() && count < 100) {
            System.out.println("count = " + count++);
            try{
                Thread.sleep(1);
            }catch (InterruptedException e){
                e.printStackTrace();
                //重新设置线程中断状态为true
                Thread.currentThread().interrupt();
            }
        }

        System.out.println("线程停止： stop thread");
    }

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new StopThread());
        thread.start();
        Thread.sleep(5);
        thread.interrupt();
    }
}